In [3]:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow import keras
In [5]:
print(tf.__version__)
In [6]:
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels),(test_images, test_labels) = fashion_mnist.load_data()
In [13]:
print("train images:",train_images.shape)
print("train labels:",len(train_labels))
print("train labels:",train_labels)
print("test images:",test_images.shape)
print("test labels:",test_labels.shape)
In [11]:
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
In [15]:
plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.show()
In [16]:
### scaling of these dataset between 0 and 255 to fit NN
train_images = train_images / 255.0
test_images = test_images / 255.0
In [19]:
### display the scaled down image version
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i],cmap=plt.cm.binary)
plt.xlabel(class_names[train_labels[i]])
plt.show()
In [21]:
### 1. Setup the layers
model = keras.Sequential([
keras.layers.Flatten(input_shape=[28,28]), ## flatten the 28x28 pixels to 1-d vector 786 rows
keras.layers.Dense(128, activation=tf.nn.relu), ## These are densely-connected, or fully-connected, neural layers with 128 nodes or neurons
keras.layers.Dense(10, activation=tf.nn.softmax) ## using softmax to classify 10 classes
])
In [22]:
### 2. compile the layers
model.compile(optimizer = 'adam',loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])
In [23]:
model.fit(train_images, train_labels, epochs=5)
Out[23]:
In [24]:
test_loss, test_acc = model.evaluate(test_images, test_labels)
print("Test Accuracy is: ",test_acc)
print("Test Loss is: ",test_loss)
It turns out, the accuracy on the test dataset is a little less than the accuracy on the training dataset. This gap between training accuracy and test accuracy is an example of overfitting. Overfitting is when a machine learning model performs worse on new data than on their training data.
In [25]:
predictions = model.predict(test_images)
In [26]:
predictions[0]
Out[26]:
A prediction is an array of 10 numbers. These describe the "confidence" of the model that the image corresponds to each of the 10 different articles of clothing
In [27]:
np.argmax(predictions[0]) ## to find the maximum confidence label
Out[27]:
In [28]:
### Plotting and understanding the prediction across test data
def plot_image(i, predictions_array, true_label, img):
predictions_array, true_label, img = predictions_array[i], true_label[i], img[i]
plt.grid(False)
plt.xticks([])
plt.yticks([])
plt.imshow(img, cmap=plt.cm.binary)
predicted_label = np.argmax(predictions_array)
if predicted_label == true_label:
color = 'blue'
else:
color = 'red'
plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
100*np.max(predictions_array),
class_names[true_label]),
color=color)
def plot_value_array(i, predictions_array, true_label):
predictions_array, true_label = predictions_array[i], true_label[i]
plt.grid(False)
plt.xticks([])
plt.yticks([])
thisplot = plt.bar(range(10), predictions_array, color="#777777")
plt.ylim([0, 1])
predicted_label = np.argmax(predictions_array)
thisplot[predicted_label].set_color('red')
thisplot[true_label].set_color('blue')
In [29]:
### plotting the first test data
i = 0
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions, test_labels)
plt.show()
In [30]:
i = 12
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions, test_labels)
plt.show()
In [31]:
# Plot the first X test images, their predicted label, and the true label
# Color correct predictions in blue, incorrect predictions in red
num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
plt.subplot(num_rows, 2*num_cols, 2*i+1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(num_rows, 2*num_cols, 2*i+2)
plot_value_array(i, predictions, test_labels)
plt.show()
In [32]:
# Grab an image from the test dataset
img = test_images[0]
print(img.shape)
# Add the image to a batch where it's the only member.
img = (np.expand_dims(img,0))
print(img.shape)
In [33]:
predictions_single = model.predict(img)
print(predictions_single)
In [34]:
plot_value_array(0, predictions_single, test_labels)
plt.xticks(range(10), class_names, rotation=45)
plt.show()
In [35]:
prediction_result = np.argmax(predictions_single[0])
print(prediction_result)